package cn.wk.org.service.impl;

import cn.wk.basic.query.BaseQuery;
import cn.wk.basic.service.impl.BaseServiceImpl;
import cn.wk.basic.utils.PageBean;
import cn.wk.basic.utils.SendMail;
import cn.wk.org.domain.Employee;
import cn.wk.org.domain.Rejected;
import cn.wk.org.domain.Shop;
import cn.wk.org.mapper.RejectedMapper;
import cn.wk.org.mapper.ShopMapper;
import cn.wk.org.service.IRejectedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;
@Service
public class RejectedServiceImpl extends BaseServiceImpl<Rejected> implements IRejectedService {
    @Autowired
    private RejectedMapper rejectedMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SendMail sendMail;

    /**
     * 查询驳回选项
     * @return
     */
    @Override
    public List<Rejected> findRejected() {
        return rejectedMapper.findRejected();
    }

    @Override
    public Shop findShopById(Long id) {
        return shopMapper.findShopById(id);
    }

    /**
     * 驳回确定
     * @param rejected
     */
    @Transactional
    @Override
    public void sendReject(Rejected rejected) throws MessagingException {
        //先根据传进来的shopid去把shop状态禁用，再发送邮件
        shopMapper.updateStateNo(rejected.getShopid());
        //通过传进来的id去查询是什么错误
        String name = rejectedMapper.findByid(rejected.getId());
        //根据id去查询商铺所属员工，然后获取其邮箱
        Shop shopById = shopMapper.findById(rejected.getShopid());

        System.out.println(shopById);

        Employee admin = shopById.getAdmin();
        String email = admin.getEmail();
        //发送邮件
        sendMail.send(email,name);
    }
}
